package com.sinothk.hm.snapshot.comm.viewModel;

import com.sinothk.hm.retrofit.HiRetrofit;
import com.sinothk.hm.snapshot.UrlConfig;
import com.sinothk.hm.snapshot.comm.config.AllApi;
import com.sinothk.hm.snapshot.comm.model.base.ResultInfo;
import com.sinothk.hm.snapshot.comm.model.domain.LoginDto;
import com.sinothk.hm.snapshot.comm.model.domain.LoginVo;
import org.greenrobot.eventbus.EventBus;
import rx.Subscriber;
import rx.schedulers.Schedulers;

public class CommViewModel {

    public void login(String account, String password) {
        HiRetrofit.init(UrlConfig.baseUrl)
                .create(AllApi.class)
                .login(new LoginVo(account, password))
                .subscribeOn(Schedulers.io())
//                .observeOn(HmSchedulers.mainThread())
                .subscribe(new Subscriber<ResultInfo<LoginDto>>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        ResultInfo<ResultInfo<LoginDto>> result = new ResultInfo<>(ResultInfo.ERROR, ResultInfo.getErrorMsg(e.getMessage()));
                        result.setEventType("login");
                        EventBus.getDefault().post(result);
                    }

                    @Override
                    public void onNext(ResultInfo<LoginDto> result) {
                        result.setEventType("login");
                        EventBus.getDefault().post(result);
                    }
                });
    }
}
